package org.apache.james.mime4j.field.address;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.ArrayList;
import org.apache.james.mime4j.field.address.parser.AddressListParser;
import org.apache.james.mime4j.field.address.parser.ParseException;

public class AddressList
{
  private ArrayList addresses;

  public AddressList(ArrayList paramArrayList, boolean paramBoolean)
  {
    if (paramArrayList != null)
      if (paramBoolean)
      {
        localArrayList = paramArrayList;
        this.addresses = localArrayList;
      }
    while (true)
    {
      while (true)
      {
        return;
        localArrayList = (ArrayList)paramArrayList.clone();
      }
      this.addresses = new ArrayList(0);
    }
  }

  public static void main(String[] paramArrayOfString)
    throws Exception
  {
    BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(System.in));
    try
    {
      System.out.print("> ");
      String str = localBufferedReader.readLine();
      if ((str.length() == 0) || (str.toLowerCase().equals("exit")) || (str.toLowerCase().equals("quit")))
      {
        System.out.println("Goodbye.");
        return;
      }
      parse(str).print();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      Thread.sleep(300L);
    }
  }

  public static AddressList parse(String paramString)
    throws ParseException
  {
    AddressListParser localAddressListParser = new AddressListParser(new StringReader(paramString));
    return Builder.getInstance().buildAddressList(localAddressListParser.parse());
  }

  public MailboxList flatten()
  {
    MailboxList localMailboxList;
    int i = 0;
    int j = 0;
    if (j < size())
    {
      if (get(j) instanceof Mailbox)
        break label44;
      i = 1;
    }
    if (i == 0)
      localMailboxList = new MailboxList(this.addresses, true);
    while (true)
    {
      while (true)
      {
        return localMailboxList;
        label44: ++j;
      }
      ArrayList localArrayList = new ArrayList();
      for (int k = 0; k < size(); ++k)
        get(k).addMailboxesTo(localArrayList);
      localMailboxList = new MailboxList(localArrayList, false);
    }
  }

  public Address get(int paramInt)
  {
    if ((paramInt < 0) || (size() <= paramInt))
      throw new IndexOutOfBoundsException();
    return ((Address)this.addresses.get(paramInt));
  }

  public void print()
  {
    for (int i = 0; i < size(); ++i)
    {
      Address localAddress = get(i);
      System.out.println(localAddress.toString());
    }
  }

  public int size()
  {
    return this.addresses.size();
  }
}